import { CrsClient } from "./crsClient"
import { config } from "../../config"

const systemInfo = wx.getSystemInfoSync()

const SELECT_TYPE = {
  NONE: 0,
  IMAGE: 1,
  VIDEO: 2,
}
const App = getApp()
Page({
  data: {
    navH: App.globalData.navHeight, //适配刘海屏
    showOverlay: true,
    showSelect: false,
    SELECT_TYPE: SELECT_TYPE,
    selectType: 0,
    imgIndex: 1,
    imgList: [],
    isShowScan:false,
    scanType:null,
    //CRS配置
    config: {
      token:App.globalData.easyArToken,
      cloudKey: "988832ecf8cf3f9c438d2bb04ad0bacc",
      clientHost: "https://cn1-crs.easyar.com:8443", //服务器一般不变
      jpegQuality: 0.7, //JPEG压缩质量，建议不低于70%
      minInterval: 1000, //最短的两次CRS请求间隔
    },
    //识别到这个数组中的ID就触发内容
    targetIds: [
      "60b1d6a2-c06f-447f-a040-3504bc6f661f",
      "36f2b130-f277-41d6-a6bd-d5f3ff944abc",
    ],

    showLoading: false,
    showLoadingText: "",
    isshow: false,
  },

  /** @type {CameraFrameListener} 相机帧回调 */
  listener: undefined,
  /** @type {HTMLCanvasElement} canvas对象 */
  canvas: undefined,

  /** @type {boolean} 是否需要持续识别，在点击“识别体验”之后和识别成功之前为true */
  runningCrs: undefined,
  /** @type {boolean} 当前是否正在进行CRS请求 */
  busy: undefined,
  /** @type {CrsClient} 负责发起CRS请求的对象 */
  crsClient: undefined,
  /** @type {number} 最后一次CRS请求的事件，用于判断是否满足最短请求间隔 */
  last: undefined,
  scanNum:0,

  onLoad: function () {
    const keySources = []
    for (var i = 0; i <= 40; i++) {
      var num = ""
      var len = i.toString().length
      if (len == 1) {
        num = "0000"
      }
      if (len == 2) {
        num = "000"
      }
      if (len == 3) {
        num = ""
      }
      keySources.push(
        "http://xcx_test.vaiwan.com/shu3/shu1_" + num + i + ".png"
      )
    }
    this.setData({
      imgList: keySources,
    })
  },
  onReady: function () {
    if (systemInfo.platform === "devtools") {
      //开发工具不会触发initdone事件，于是在onReady手动触发
      //this.onCameraInit()
    }
    this.scan()
   // this.onCameraInit()
    console.log(App.globalData.easyArToken)
    this.getToken()
  },
  //获取token
  getToken(){
    var _this = this
    wx.request({
      url: `${config.api_blink_url}/token.json?time=${new Date().getTime()}`,
      method: "get",
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        this.setData({
          config:{
            token:res.data.token,
            cloudKey: "988832ecf8cf3f9c438d2bb04ad0bacc",
            clientHost: "https://cn1-crs.easyar.com:8443", //服务器一般不变
            jpegQuality: 0.7, //JPEG压缩质量，建议不低于70%
            minInterval: 1000, //最短的两次CRS请求间隔
          }
        });
        console.log(this.data.config.token)
        //this.onCameraInit()
      }
    })
  },
  showLoading(text) {
    this.setData({
      showLoading: true,
      showLoadingText: text,
    })
  },
  hideLoading() {
    this.setData({
      showLoading: false,
    })
  },

  //图像识别部分：

  onShow: function () {
    if (this.listener) this.listener.start() //页面隐藏时相机帧的监听会自动停止，但恢复展示时不会自动启动，这里手动启动
  },

  onCameraInit: function () {
    //找到canvas对象
    const query = wx.createSelectorQuery()
    query
      .select("#capture")
      .fields({ node: true })
      .exec((res) => {
        const canvas = res[0].node
        //设置canvas内部尺寸为480*640，frame-size="medium"的设置下相机帧大多是480*640
        canvas.width = 480
        canvas.height = 640
        this.canvas = canvas

        this.crsClient = new CrsClient(this.data.config, this.canvas)

        //开始监听相机帧
        let cameraContext = wx.createCameraContext()
        this.listener = cameraContext.onCameraFrame((frame) => {
          if (!this.canvas) return
          let canvas = this.canvas
          //如果尺寸不匹配，就修改canvas尺寸以适应相机帧
          if (canvas.width !== frame.width || canvas.height !== frame.height) {
            canvas.width = frame.width
            canvas.height = frame.height
          }

          this.queryImage(frame)
        })
        this.listener.start()
      })
  },

  queryImage: function (frame) {
    if (!this.runningCrs || this.busy || !this.crsClient) return

    //最短的两次CRS请求间隔
    let now = new Date().getTime()
    if (this.last && now - this.last < this.data.config.minInterval) return
    this.last = now

    this.busy = true //如果正在进行CRS请求，就不允许再次请求

    this.crsClient
      .queryImage(frame)
      .then((res) => {
        if (!this.runningCrs) return //避免在停止后仍然触发
        let result = res && res.result
        if (!result) return

        if (result.target) {
          console.log("识别成功", result.target.targetId)
          //如果待触发的id列表中存在识别到的这个id，就触发
          if (
            this.data.targetIds.find(
              (targetId) => targetId === result.target.targetId
            )
          ) {
            // this.onResult(result.target)
            if (result.target.targetId === this.data.targetIds[0]) {
              this.go()
            } else {
              this.goTrain()
            }
          }
        } else {
          console.log(result)
          this.scanNum++
          if(this.scanNum===30){
            if(this.data.scanType ==='tree'){
              this.go()
            }else {
              this.goTrain()
            }
          }
        }
        this.busy = false
      })
      .catch((e) => {
        this.busy = false
        console.log(e)
        if(this.data.scanType ==='tree'){
          this.go()
        }else {
          this.goTrain()
        }
      }) //小程序iOS端不支持finally，所以在then和catch里分别设置busy = false
  },

  onResult: function (target) {
    this.runningCrs = false
    this.hideLoading()
    console.log("触发内容!")
    if (target.meta) {
      console.log("meta base64:", target.meta)
    }
    this.setData({
      showOverlay: false,
      showContent: true,
      selectType: SELECT_TYPE.IMAGE,
    })
  },

  //界面：

  back: function () {
    this.runningCrs = false
    this.setData({
      showOverlay: true,
      showContent: false,
      selectType: SELECT_TYPE.NONE,
    })
    this.hideLoading()
  },

  experience: function () {
    this.setData({
      showOverlay: false,
      showContent: true,
      selectType: SELECT_TYPE.IMAGE,
      showLoading: true,
      isshow: true,
    })
    setTimeout(() => {
      this.setData({
        showLoading: false,
        isshow: false,
      })
    }, 1000)
    this.animationImag()
  },

  scan: function () {
    this.runningCrs = true
    this.setData({
      showOverlay: false,
      showContent: false,
      selectType: SELECT_TYPE.NONE,
    })
    this.showLoading("识别中")
    //this.onCameraInit()
    // this.setData({
    //   showOverlay: false,
    //   showContent: false,
    // })
  },

  download: function () {
    wx.saveImageToPhotosAlbum({
      filePath: "/images/namecard.jpg",
      success: (res) => {
        wx.showToast({ title: "已保存到相册", icon: "none" })
      },
      fail: (res) => {
        wx.showToast({ title: "保存失败", icon: "none" })
      },
    })
  },

  selectContent: function (e) {
    this.setData({
      selectType: e.currentTarget.dataset.contenttype,
    })
  },
  imageLoad(ev) {},
  animationImag() {
    var _this = this
    var i = 1
    this.timer = setInterval(function () {
      i++
      i = i % 40
      _this.setData({
        imgIndex: i,
      })
    }, 150)
  },
  scanTree:function(){
    this.setData({
      scanType:'tree',
      isShowScan:true
    })
    this.busy = false
    this.scanNum = 0
    this.onCameraInit()
  },
  scanTrain:function(){
    this.setData({
      scanType:'train',
      isShowScan:true
    })
    this.busy = false
    this.scanNum = 0
    this.onCameraInit()
  },
  go: function () {
    wx.redirectTo({
      url: "/pages/content/index",
    })
  },
  goTrain: function () {
    wx.redirectTo({
      url: "/pages/train/index",
    })
  },
  back:function(){
    this.setData({
      isShowScan:false
    })
    this.busy = true
  },
  onJumpMin() {
    wx.navigateToMiniProgram({
      appId: "wxe9bc7c69463b5455", // 需要跳转到指定的小程序appid
      path: "", // 打开的页面
      envVersion: "release",
      success(res) {
        // 打开成功
        console.log(res)
      },
    })
  },
})
